package com.mobilesales.android.adapters;

import java.util.List;

import com.mobilesales.android.R;
import com.mobilesales.android.model.tasks.TaskStatus;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TaskStatusAdapter extends ArrayAdapter<TaskStatus> {

	private List<TaskStatus> items;
	
	public TaskStatusAdapter(Context context, int resource, List<TaskStatus> objects) {
		super(context, resource, R.id.selectItemTxt, objects);
		items = objects;
	}

	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					"layout_inflater");
			v = vi.inflate(R.layout.select_list_item, null);
		}

		TaskStatus status = null;
		if ((position >= 0) && (position < this.items.size())) {
			status = this.items.get(position);
		}

		if (status != null) {
			TextView nameField = (TextView) v.findViewById(R.id.selectItemTxt);
			nameField.setText(status.Name);
		}
		return v;
	}
	
	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return getView(position, convertView, parent);
	}

	public int getPosition(int groupId) {
		if (this.items != null) {
			for (int i = 0; i < this.items.size(); i++) {
				if (this.items.get(i).Id == groupId)
					return i;
			}
		}
		return -1;
	}
}
